package common.converter;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.convert.converter.Converter;

import common.constant.GlobalConstant;
import common.util.DateUtils;

/**
 * 日期类型数据转换器，将String转换成日期对象
 * 
 * @author liuxj
 * @version 1.0.0 2017-07-15
 */
public class StringToDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String date) {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        date = date.trim();
        
        if (date.length() < 10 || date.equalsIgnoreCase("null")) {
            return null;
        }
        
        if (date.length() > 10) {
            return DateUtils.strToDate(date, GlobalConstant.DATE_TIME_PATTERN);
        } else {
            return DateUtils.strToDate(date, GlobalConstant.DATE_PATTERN);
        }
    }

}
